<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>Node 信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">


                        <div class="layui-inline">
                            <label class="layui-form-label">k8s</label>
                            <div class="layui-input-inline">
                                <select name="k8sId" id="k8sId">
                                </select>
                            </div>

                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;


        $.ajax({
            url: '../k8s/record/list',
            type: 'POST',
            data: {},
            async: false,
            success: function (result) {

                if (result.code === 10000) {
                    $('#k8sId').empty()
                    const selectList = result.data || []
                    selectList.forEach((selectItem, selectIndex) => {
                        $('#k8sId').append(`<option value="${selectItem.id}" ${selectIndex === 0 ? 'selected' : ''} >${selectItem.clusterName}</option>`)
                    })
                    // $('#k8sId').append(`<option value="1"  >123</option>`)
                    form.render()

                    console.log($('#k8sId').val())

                } else {
                    parent.layer.alert(result.msg)
                }

            }
        })


        window.tableResult = table.render({
            elem: '#currentTableId',
            url: '../node/list',
            // toolbar: '#toolbarDemo',
            where: {
                k8sId: $('#k8sId').val(),
                version: '2023-05-12'
            },
            // defaultToolbar: ['filter', 'exports', 'print', {
            //     title: '提示',
            //     layEvent: 'LAYTABLE_TIPS',
            //     icon: 'layui-icon-tips'
            // }],
            cols: [[
                {field: 'name', title: 'name', minWidth: 130},
                {
                    field: 'name', title: 'Status', width: 130, templet: row => {
                        const conditionVos = row.conditionVos
                        const status = conditionVos.find(item => item.type === 'Ready')?.status || ''
                        let html = '';
                        html += status;
                        return html
                    }
                },
                // {
                //     title: 'Ready', width: 130, templet: row => {
                //
                //         let html = ''
                //         const conditionVos = row.conditionVos
                //         if (conditionVos) {
                //             const target = conditionVos.find(ele => ele.type === 'Ready')
                //             if (target) {
                //                 html = target.status
                //             }
                //         }
                //
                //
                //         return html
                //     }
                //
                //
                // },
                {field: 'roles', title: 'Roles', width: 130},
                {
                    field: '', width: 180, title: 'Internal-Ip', templet: row => {
                        const addresses = row.addresses || []
                        let html = ''
                        const targetAddress = addresses.find(item => item.type === 'InternalIP')
                        if (targetAddress) {
                            html += targetAddress.address
                        }
                        return html
                    }
                },
                {field: 'kubeletVersion', width: 120, title: 'Version'},
                {field: 'osImage', width: 180, title: 'Os-Image'},
                {field: 'kernelVersion', width: 180, title: 'Kernel Version'},
                {field: 'containerRuntimeVersion', width: 180, title: 'Container Runtime'},

                {field: 'creationTime', width: 180, title: 'create time', align: 'center'},
                {
                    title: '操作', width: 100, align: "center", templet: row => {
                        return `<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail">详情</button>`
                    }
                }
            ]],
            // limits: [10, 15, 20, 25, 50, 100],
            // limit: 15,
            page: false,
            skin: 'line',
            limit: Number.MAX_VALUE,
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code === 10000 ? 0 : 500, //解析接口状态
                    "msg": res.code === 10000 ? '查询成功' : res.msg, //解析提示文本
                    "count": res.data?.length || 0, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            //执行搜索重载
            table.reload('currentTableId', {

                where: {
                    k8sId: $('#k8sId').val()
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                window.k8sId = $('#k8sId').val()
                var index = layer.open({
                    title: '添加service',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1000px', '60%'],
                    content: '../page/service-add.html',
                });
                $(window).on("resize", function () {
                    // layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除么，这将导致无法从外部访问pod', function (index) {
                    // $.ajax({
                    //     url: '../node/delete',
                    //     type: 'POST',
                    //     data: {
                    //         k8sId: $('#k8sId').val(),
                    //         name: data.name
                    //     },
                    //     success: function (result) {
                    //         if (result.code === 10000) {
                    //             layer.msg('删除成功')
                    //             //执行搜索重载
                    //             table.reload('currentTableId', {
                    //
                    //                 where: {
                    //                     k8sId: $('#k8sId').val()
                    //                 }
                    //             }, 'data');
                    //         } else {
                    //             layer.msg(result.msg)
                    //         }
                    //     }
                    // })
                    layer.close(index);
                });
            } else if (obj.event ==='detail' ) {
                window.nodeName = data.name
                window.k8sId = $('#k8sId').val()
                console.log(123)

                var index = layer.open({
                    title: 'Node 详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1300px', '90%'],
                    content: '../page/node-detail.html',
                });
            }
        });

    });
</script>

</body>
</html>
